import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget, QVBoxLayout, QWidget, QLabel, QToolTip
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QFont, QPalette, QPixmap
from PyQt5.QtCore import Qt


class QLabelDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # label1 = QLabel(self)
        # label2 = QLabel(self)
        # label3 = QLabel(self)
        # label4 = QLabel(self)
        # label1.setText("<font color=yellow>这是一个文本标签.</font>")
        # # 设置自动填充背景
        # label1.setAutoFillBackground(True)
        # # 创建调色板
        # palette = QPalette()
        # palette.setColor(QPalette.Window, Qt.blue)  # 设置背景色
        # label1.setPalette(palette)
        # # 设置文本居中对齐
        # label1.setAlignment(Qt.AlignCenter)
        # label2.setText("<a href='#'>欢迎使用PyQt5</a>")
        # label3.setAlignment(Qt.AlignCenter)
        # label3.setToolTip('这是一个图片标签')
        # label3.setPixmap(QPixmap("D:\python-project\py14.PyQt5\imgs\万圣节-蝙蝠.ico"))
        # # 屏蔽事件打开扩展链接
        # # 如果设为True，用浏览器打开网页，如果设为False，调用槽函数
        # label4.setOpenExternalLinks(True)
        # label4.setText("<a href='https://item.jd.com/12417265.html'>《Python从菜鸟到高手》</a>")
        # label4.setAlignment(Qt.AlignRight)
        # label4.setToolTip("这是一个超链接")
        # # 垂直布局
        # vbox = QVBoxLayout()
        # vbox.addWidget(label1)
        # vbox.addWidget(label2)
        # vbox.addWidget(label3)
        # vbox.addWidget(label4)
        # # 绑定槽函数
        # label2.linkHovered.connect(self.linkHovered)
        # label4.linkActivated.connect(self.linkClicked)
        # # 设置布局
        # self.setLayout(vbox)
        # self.setWindowTitle('QLabel控件演示')
    # def linkHovered(self):
    #     print("当鼠标滑过label2标签时，触发事件")
    # def linkClicked(self):
    #     print("当鼠标点击label4标签时，触发事件")


        # """QLabel伙伴关系"""
        # # 设置伙伴关系控件
        # nameLabel = QLabel('&Name', self)
        # nameLineEdit = QLineEdit(self)
        # nameLabel.setBuddy(nameLineEdit)
        #
        # pwdLabel = QLabel('&pwd', self)
        # pwdLineEdit = QLineEdit(self)
        # pwdLabel.setBuddy(pwdLineEdit)
        #
        # btnOk = QPushButton('&Ok')
        # btnCancel = QPushButton('&Cancel')
        # # 栅格布局
        # mainLayout = QGridLayout(self)
        # mainLayout.addWidget(nameLabel, 0, 0)
        # mainLayout.addWidget(nameLineEdit, 0, 1, 1, 2)
        # mainLayout.addWidget(pwdLabel, 1, 0)
        # mainLayout.addWidget(pwdLineEdit, 1, 1, 1, 2)
        # mainLayout.addWidget(btnOk, 2, 1)
        # mainLayout.addWidget(btnCancel, 2, 2)


        """回显模式"""
        self.setWindowTitle("文本输入框回显模式")
        formLayout = QFormLayout()

        normalLineEdit = QLineEdit()
        noEchoLineEdit = QLineEdit()
        passwordLineEdit = QLineEdit()
        passwordEchoOnEditLineEdit = QLineEdit()

        formLayout.addRow("Normal", normalLineEdit)
        formLayout.addRow("NoEcho", noEchoLineEdit)
        formLayout.addRow("Password", passwordLineEdit)
        formLayout.addRow("PasswordEchoOnEdit", passwordEchoOnEditLineEdit)
        # 设置placeholdertext（在文本框无内容时显示提示信息）
        normalLineEdit.setPlaceholderText("Normal")
        noEchoLineEdit.setPlaceholderText("NoEcho")
        passwordLineEdit.setPlaceholderText("PasswordLineEdit")
        passwordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEditLineEdit")

        # 设置模式
        normalLineEdit.setEchoMode(QLineEdit.Normal)
        noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
        passwordLineEdit.setEchoMode(QLineEdit.Password)
        passwordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)

        # 应用表单布局
        self.setLayout(formLayout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLabelDemo()
    main.show()
    sys.exit(app.exec_())
